<?php
/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of AccountController
 *
 * @author Luong Trong Hieu
 */
class Admin_AccountController extends Zend_Controller_Action {
    function indexAction() {
        $this->view->title = "User index";

        $page = $this->_request->getParam('page', 1);
        $offset = 10;
        $acc = new AccountDAO();
        $user = $acc->getUserPage($page, $offset);
        $this->view->data = $user;
    }

    function viewAction() {
        $this->view->title = "User Info";
        
        $id = $this->_request->getParam('id', 0);
        if($id > 0)
        {
            $acc = new AccountDAO();
            $user = $acc->getUser($id);
            if($user)
            {
                $this->view->id = $user['id'];
                $this->view->username = $user['username'];
                $this->view->fullname = $user['fullname'];
                $this->view->byear = $user['b_year'];
                $this->view->email = $user['email'];
                $this->view->address = $user['address'];
                $this->view->status = $user['status'];

                $roleOb = new RoleDAO();
                $role = $roleOb->getRole($user['role_id']);
                $role = $role->toArray();
                $this->view->role = $role['name'];

                $typeOb = new TypeDAO();
                $type = $typeOb->getType($user['type_id']);
                $type = $type->toArray();
                $this->view->type = $type['name'];
            }
            else
                $this->view->message = "User not found!<br />";
        }
        else
            $this->view->message = "User not found! <br />";
    }

    function updateAction() {
        $this->view->title="Edit user";

        if(isset($_POST['submit']))
        {
            $acc = new AccountDAO();
            /*echo $_POST['udusername'];
            echo $_POST['udfullname'];
            echo $_POST['password'];
            echo $_POST['udemail'];
            echo $_POST['udaddress'];
             *
             */

            $id = $_POST['id'];

            if($_POST['password'] == "")
            {
                $data = array(
                    'username' => $_POST['udusername'],
                    'fullname' => $_POST['udfullname'],
                    'b_year' => $_POST['udbyear'],
                    'sex' => $_POST['udsex'],
                    'email' => $_POST['udemail'],
                    'address' => $_POST['udaddress'],
                    'role_id' => $_POST['udrole'],
                    'type_id' => $_POST['udtype'],
                    'status' => $_POST['udstatus'],
                 );
             }
            else
            {
                $data = array(
                    'username' => $_POST['udusername'],
                    'password' => $_POST['password'],
                    'fullname' => $_POST['udfullname'],
                    'b_year' => $_POST['udbyear'],
                    'sex' => $_POST['udsex'],
                    'email' => $_POST['udemail'],
                    'address' => $_POST['udaddress'],
                    'role_id' => $_POST['udrole'],
                    'type_id' => $_POST['udtype'],
                    'status' => $_POST['udstatus'],
                 );
            }

            if($acc->updateUser($id, $data))
             {
                $this->view->message="Update complete!<br />";
             }
            else
              {
                  $this->view->message="Username or email
                    has already been used!<br />";
              }

        }
        else
        {
            $id = $this->_request->getParam('id', 0);
            if($id > 0)
            {
                $acc = new AccountDAO();
                $accInfo = $acc->getUser($id);
                if($accInfo)
                {
                    $this->view->id = $accInfo['id'];
                    $this->view->username = $accInfo['username'];
                    $this->view->fullname = $accInfo['fullname'];
                    $this->view->sex = $accInfo['sex'];
                    $this->view->byear = $accInfo['b_year'];
                    $this->view->email = $accInfo['email'];
                    $this->view->address = $accInfo['address'];
                    $this->view->role_id = $accInfo['role_id'];
                    $this->view->type_id = $accInfo['type_id'];
                    $this->view->status = $accInfo['status'];
                }
                else
                    {
                        $this->view->id = 0;
                        $this->view->message = "No user found!<br />";
                    }
            }
            else
                $this->view->message = "No user found!<br />";
        }
    }

    function deleteAction(){
        $id = $this->_request->getParam('id', -1);
        if($id > 0)
        {
            $this->view->title = "Delete User";
            $acc = new AccountDAO();
            $acc->deleteUser($id);
            $this->view->message = "Deleted!<br />";
        }
        else
            $this->view->message = "Account not found!<br />";
    }

    function searchAction() {
        
    }
}
?>
